package com.ly.heart.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.ly.common.core.domain.AjaxResult;
import com.ly.common.exception.CustomException;
import com.ly.common.utils.DateUtils;
import com.ly.common.utils.SecurityUtils;
import com.ly.common.utils.erp.ReturnPage;
import com.ly.common.utils.erp.SnowflakeIdWorker;
import com.ly.heart.mapper.AssetsChangeMethodMapper;
import com.ly.heart.service.IAssetsChangeMethodService;
import com.ly.heart.util.CurrentUserUtil;
import org.springframework.stereotype.Service;
import com.ly.heart.domain.AssetsChangeMethodEntity;

import javax.annotation.Resource;

/**
 * 资产变动方式Service业务层处理
 *
 * @author cxl
 * @date 2024-09-03
 */
@Service
public class AssetsChangeMethodServiceImpl extends ServiceImpl<AssetsChangeMethodMapper,AssetsChangeMethodEntity> implements IAssetsChangeMethodService {

    @Resource
    private AssetsChangeMethodMapper assetsChangeMethodMapper;


    @Override
    public AjaxResult getAssetsChangeMethodList(AssetsChangeMethodEntity assetsChangeMethod){
        List<AssetsChangeMethodEntity> list = assetsChangeMethodMapper.selectAssetsChangeMethodEntityList(assetsChangeMethod);
        Page<AssetsChangeMethodEntity> page = new Page<>(assetsChangeMethod.getPage(),assetsChangeMethod.getLimit());
        return AjaxResult.success(ReturnPage.getReturnPage(page,list));
    }

    /**
     * 查询资产变动方式
     *
     * @param id 资产变动方式ID
     * @return 资产变动方式
     */
    @Override
    public AssetsChangeMethodEntity selectAssetsChangeMethodEntityById(String id)
    {
        return assetsChangeMethodMapper.selectAssetsChangeMethodEntityById(id);
    }

    /**
     * 查询资产变动方式列表
     *
     * @param assetsChangeMethodEntity 资产变动方式
     * @return 资产变动方式
     */
    @Override
    public List<AssetsChangeMethodEntity> selectAssetsChangeMethodEntityList(AssetsChangeMethodEntity assetsChangeMethodEntity)
    {
        return assetsChangeMethodMapper.selectAssetsChangeMethodEntityList(assetsChangeMethodEntity);
    }

    /**
     * 新增资产变动方式
     *
     * @param assetsChangeMethod 资产变动方式
     * @return 结果
     */
    @Override
    public int insertAssetsChangeMethodEntity(AssetsChangeMethodEntity assetsChangeMethod)
    {
        assetsChangeMethod.setId(SnowflakeIdWorker.getInstance().nextStringId());
        assetsChangeMethod.setCreateBy(SecurityUtils.getLoginUser().getUser().getUserId());
        assetsChangeMethod.setCreateTime(DateUtils.getNowDate());
        return assetsChangeMethodMapper.insertAssetsChangeMethodEntity(assetsChangeMethod);
    }

    /**
     * 修改资产变动方式
     *
     * @param assetsChangeMethod 资产变动方式
     * @return 结果
     */
    @Override
    public int updateAssetsChangeMethodEntity(AssetsChangeMethodEntity assetsChangeMethod)
    {
        assetsChangeMethod.setUpdateBy(SecurityUtils.getLoginUser().getUser().getUserId());
        assetsChangeMethod.setUpdateTime(DateUtils.getNowDate());
        return assetsChangeMethodMapper.updateAssetsChangeMethodEntity(assetsChangeMethod);
    }

    /**
     * 批量删除资产变动方式
     *
     * @param ids 需要删除的资产变动方式ID
     * @return 结果
     */
    @Override
    public int deleteAssetsChangeMethodEntityByIds(String[] ids)
    {
        return assetsChangeMethodMapper.deleteAssetsChangeMethodEntityByIds(ids);
    }

    /**
     * 删除资产变动方式信息
     *
     * @param id 资产变动方式ID
     * @return 结果
     */
    @Override
    public int deleteAssetsChangeMethodEntityById(String id)
    {
        return assetsChangeMethodMapper.deleteAssetsChangeMethodEntityById(id);
    }

    /**
     * 禁用/反禁用
     *
     * @param id
     * @return
     */
    @Override
    public AjaxResult disable ( String id ) {
        AssetsChangeMethodEntity assetsChangeMethod = this.getById(id);
        if ( assetsChangeMethod == null ) {
            throw new CustomException( "数据不存在！" );
        }
        boolean update = this.update ( new LambdaUpdateWrapper<AssetsChangeMethodEntity>().
                set ( AssetsChangeMethodEntity :: getIsDisabled, assetsChangeMethod.getIsDisabled () == 1 ? 2 : 1 ).
                set ( AssetsChangeMethodEntity :: getUpdateTime, DateUtils.getNowDate () ).
                set ( AssetsChangeMethodEntity :: getUpdateBy, SecurityUtils.getLoginUser ().getUser ().getUserId () ).
                eq ( AssetsChangeMethodEntity :: getId, id ) );
        if ( ! update ) {
            throw new CustomException ( assetsChangeMethod.getIsDisabled () == 1 ? "禁用失败！" : "解禁失败！" );
        }
        if( assetsChangeMethod.getIsDisabled () == 1 ){
            this.update(new LambdaUpdateWrapper<AssetsChangeMethodEntity>().eq(AssetsChangeMethodEntity::getId, id)
            .set(AssetsChangeMethodEntity::getUnbanBy, SecurityUtils.getLoginUser().getUser().getUserId())
            .set(AssetsChangeMethodEntity::getUnbanDept, CurrentUserUtil.getDeptId()));
        }
        if(assetsChangeMethod.getIsDisabled() == 2){
            this.update(new LambdaUpdateWrapper<AssetsChangeMethodEntity>().eq(AssetsChangeMethodEntity::getId, id)
                .set(AssetsChangeMethodEntity::getDisableBy, SecurityUtils.getLoginUser().getUser().getUserId())
                .set(AssetsChangeMethodEntity::getDisableDept, CurrentUserUtil.getDeptId()));
        }
        return AjaxResult.success ( assetsChangeMethod.getIsDisabled () == 1 ? "禁用成功" : "解禁成功" );
    }

}
